//
//  QXGuideCellectionVC.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/26.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class QXGuideCellectionVC: UICollectionViewController {

    let layout = UICollectionViewFlowLayout()
    let imgCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.registerClass(QXGuideCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! QXGuideCell
        cell.backgroundColor = UIColor.blueColor()
        cell.iconIdx = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        if collectionView.indexPathsForVisibleItems().last?.item == imgCount - 1 {
            let lastCell = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: imgCount - 1, inSection: 0)) as! QXGuideCell
            lastCell.showStartBtn()
        }

    }
    
}


class QXGuideCell: UICollectionViewCell {
    
    var iconIdx: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(iconIdx + 1)")
            hideStartBtn()
        }
    }
    
    lazy var iconView: UIImageView = {
        return UIImageView()
    }()
    
    lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.bounds = CGRectMake(0, 0, 100, 50)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "btnClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    func btnClicked() {
        // bool 值表示是否跳转到主界面
        NSNotificationCenter.defaultCenter().postNotificationName(QXAppDelegateChangeRootViewControllerNotification, object: true)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        addSubview(startBtn)
        
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -100))

        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func hideStartBtn() {
        startBtn.hidden = true;
    }
    
    func showStartBtn() {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.4, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: { (_) -> Void in
        })
    }
    
}


